package com.justech.mes.v2.dao;

import com.justech.mes.v2.model.MesEquip;
import org.springframework.data.annotation.Transient;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lin.jia
 */
@Repository
public interface MesEquipRepository extends JpaSpecificationExecutor<MesEquip>, JpaRepository<MesEquip, Long> {

    /**
     * 根据类型、设备名称、数据类型查询数据
     *
     * @param equipmentName
     * @param sn
     * @param dataType
     * @return
     */
    List<MesEquip> findByEquipmentNameAndSnAndDataType(String equipmentName, String sn, String dataType);


    /**
     * 查询表头
     *
     * @param equipmentName
     * @param sn
     * @param dataType
     * @return
     */
    @Query(value = " select distinct title from mes_v2_equipment where equipment_name=?1 and sn =?2 and data_type = ?3 ", nativeQuery = true)
    List<String> findTitle(String equipmentName, String sn, String dataType);


    /**
     * 查询每一次的对应表头的数据
     *
     * @param equipmentName
     * @param sn
     * @param dataType
     * @param title
     * @param index
     * @return
     */
    @Query(value = "select title_data from mes_v2_equipment where  equipment_name = ?1 and  sn =?2 and  data_type =?3 and title= ?4 and current_index =?5 and is_repeat=?6", nativeQuery = true)
    Double findData(String equipmentName, String sn, String dataType, String title, Integer index, Integer isRepeat);

    /**
     * 查询基础数据
     *
     * @param equipmentName
     * @param sn
     * @param index
     * @param dataType
     * @param isRepeat
     * @return
     */
    MesEquip findFirstByEquipmentNameAndSnAndIndexAndDataTypeAndIsRepeat(String equipmentName, String sn, Integer index, String dataType, Integer isRepeat);

    /**
     * 查看是否是重复数据
     *
     * @param equipmentName
     * @param sn
     * @param index
     * @param dataType
     * @param isRepeat
     * @param title
     * @return
     */
    List<MesEquip> findFirstByEquipmentNameAndSnAndIndexAndDataTypeAndIsRepeatAndTitle(String equipmentName, String sn, Integer index, String dataType, Integer isRepeat, String title);


    /**
     * 根据信息查询数据
     *
     * @param equipmentName
     * @param sn
     * @param dataType
     * @return
     */
    ArrayList<MesEquip> findByEquipmentNameAndSnAndDataTypeAndTitle(String equipmentName, String sn, String dataType, String title);

    /**
     * 更新为完成状态
     *
     * @param equipmentName
     * @param sn
     * @param dataType
     * @param title
     */
    @Transient
    @Modifying
    @Query(value = "update mes_v2_equipment set is_ok=1 where equipment_name=?1 and sn=?2 and data_type=?3 and title=?4", nativeQuery = true)
    void updateIsOkV2(String equipmentName, String sn, String dataType, String title);


    /**
     * 查询所有未完成需要删除的记录
     *
     * @param equipmentName
     * @param sn
     * @param dataType
     * @param isOk
     * @return
     */
    List<MesEquip> findByEquipmentNameAndSnAndDataTypeNotAndIsOkNot(String equipmentName, String sn, String dataType, Integer isOk);


    /**
     * 根据条件查询公差
     *
     * @param equipmentName
     * @param dataType
     * @param title
     * @param index
     * @return
     */
    @Query(value = " SELECT title_data from  mes_v2_equipment  where  equipment_name = ?1 AND data_type = ?2  AND title = ?3 and current_index != ?4 and is_repeat=0", nativeQuery = true)
    List<Double> findTolerances(String equipmentName, String dataType, String title, int index);
}
